package com.heima.article.service.impl;



import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.heima.article.dao.ApArticleConfigDao;
import com.heima.article.dao.ApArticleContentDao;
import com.heima.article.dao.ApArticleDao;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.service.ArticleService;
import com.heima.common.constants.KafkaMessageConstants;
import com.heima.model.common.article.constants.ArticleContants;
import com.heima.model.common.article.entity.ApArticle;
import com.heima.model.common.article.entity.ApArticleConfig;
import com.heima.model.common.article.entity.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.search.SearchArticleVo;
import com.heima.utils.common.SnowflakeIdWorker;
import org.apache.commons.lang.StringUtils;


import org.springframework.beans.BeanUtils;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ApArticleDao apArticleDao;
    @Resource
    private ApArticleConfigDao apArticleConfigDao;
    @Resource
    private ApArticleContentDao apArticleContentDao;
    @Resource
    private  SnowflakeIdWorker snowflakeIdWorker;
    @Resource
    private  ArticleFreemarkerServiceImpl articleFreemarkerServiceImpl;
    @Resource
    private KafkaTemplate kafkaTemplate;

    @Override
    public ResponseResult save(ArticleDto dto) {
        if (dto==null || dto.getContent()==null) {
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        ApArticle apArticle1 = apArticleDao.queryByNewsId(dto.getNewsId());
        if (apArticle1==null) {
        //新增

         apArticle.setId(snowflakeIdWorker.nextId());
        apArticleDao.insert(apArticle);

        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(apArticle.getId());
        apArticleConfig.setCreatedTime(new Date());
        apArticleConfig.setUpdatedTime(new Date());
        apArticleConfig.setEnable(1);
        apArticleConfig.setIsDelete(0);
        apArticleConfig.setIsComment(1);
        apArticleConfig.setIsForward(1);
        apArticleConfigDao.insert(apArticleConfig);


        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(apArticle.getId());
        apArticleContent.setContent(dto.getContent());
        apArticleContent.setCreatedTime(new Date());
        apArticleContent.setUpdatedTime(new Date());
        apArticleContentDao.insert(apArticleContent);
        }
        //文章已存在, 修改
        else {
            //修改文章
            apArticle.setId(apArticle1.getId());
            apArticleDao.update(apArticle);

            ApArticleContent apArticleContent = apArticleContentDao.queryById(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setUpdatedTime(new Date());
            apArticleContentDao.update(apArticleContent);

        }
        //生成静态页面保存到minio中
        String url = articleFreemarkerServiceImpl.buildArticle2Minio(apArticle.getId(), dto.getContent());
        apArticle.setStaticUrl(url);
        apArticle.setUpdatedTime(new Date());
        apArticleDao.update(apArticle);

        //后端文章数据库数据增加需要同步到elasticsearch中
        SearchArticleVo searchArticleVo = new SearchArticleVo();
        BeanUtils.copyProperties(apArticle,searchArticleVo);
        searchArticleVo.setContent(dto.getContent());
        kafkaTemplate.send(KafkaMessageConstants.AP_ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(searchArticleVo));

        return ResponseResult.okResult(apArticle.getId());
    }

    @Override
    public ResponseResult load(ArticleHomeDto dto, short loadType) {
        if (dto.getSize()==0) {
            dto.setSize(10);
        }
        dto.setSize(Math.min(dto.getSize(), 50));
        if (StringUtils.isNotBlank(dto.getTag())) {
            dto.setTag(ArticleContants.DEFAULT_TAG);
        }
        if (dto.getMaxBehotTime()==null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime()==null) {
            dto.setMinBehotTime(new Date());
        }

        Map<String, Object> map = BeanUtil.beanToMap(dto);
        map.put("loadType",loadType);
        List<ApArticle> apArticles = apArticleDao.queryByLoad(map);


        return ResponseResult.okResult(apArticles);
    }




}
